<?php
class ControllerModuleWatermark extends Controller {
	private $error = array(); 
	
	public function index() {   
		
		$this->load->model('module/watermark');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			
			$success = false;
			
			if(isset($this->request->post['action']))
			{
				
				if($this->request->post['action']== 'store')
				{
					$success = ($this->model_module_watermark->updateWatermarkStatus($this->request->post['watermark_status'])) && ($this->model_module_watermark->updateApplySetting($this->request->post['store_watermark']));
					
				} else if ($this->request->post['action']== 'watermark' && isset($this->request->post['watermark']))
				{
					$success = $this->model_module_watermark->updateWatermark($this->request->post['watermark']);
				}
			}
			
			if($success)
			{
			
				$this->model_module_watermark->clearCache(DIR_IMAGE . 'cache');	
						
				$this->session->data['success'] = 'Success: You have modified Watermark Settings!';
							
				$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
			}
		}
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Home',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Modules',
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Watermark Modification',
			'href'      => $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['create'] = $this->url->link('module/watermark/add', 'token=' . $this->session->data['token'], 'SSL');		
		
		$this->data['apply'] = $this->url->link('module/watermark/apply', 'token=' . $this->session->data['token'], 'SSL');		
		
		$this->data['token'] = $this->session->data['token'];
		
		
		if (isset($this->request->post['watermark_status'])) {
			$this->data['watermark_status'] = $this->request->post['watermark_status'];
		} else {
			$this->data['watermark_status'] = $this->config->get('watermark_status');
		}	
		
		if(isset($this->request->post['store_watermark']))
			$this->data['storeSetting'] = $this->request->post['store_watermark'];
		else
			$this->data['storeSetting'] = $this->model_module_watermark->getApplySetting();
		
		$this->data['watermark_text_fonts'] = array();
		
		$watermark_text_fonts = glob("../fonts/*.ttf");
		
		foreach($watermark_text_fonts as $file)
		{
			$this->data['watermark_text_fonts'][] = substr( $file, ( strrpos( $file, "/" ) +1 ) );
		}		
		
		$results = $this->model_module_watermark->getWatermarks();
		
		$this->data['watermarks'] = array();
		foreach($results as $result)
		{
			if($result['watermark_type'])
			{
				$watermark_type = 'Image';
				$watermark_src = '../image/'.$result['watermark_image'];			
			}
			else
			{
				$watermark_type = 'Text';
				
				$watermark_text = $result['watermark_text'];
				
				$watermark_src = 'index.php?route=module/watermark/generate&token='. $this->session->data['token'] . "&font=" . $watermark_text['font']."&text=" . $watermark_text['text']."&color=" . $watermark_text['color']."&size=" . $watermark_text['font_size']."&alpha=" . $watermark_text['opacity'];
			}
			$this->data['watermarks'][] = array(
				"type"		=> $watermark_type,
				"src" 		=> $watermark_src,
				"details"	=> $result,
				"edit_url"  => $this->url->link('module/watermark/update', 'token=' . $this->session->data['token'] . '&wid='.$result['wid'], 'SSL')
			);
		}

		$this->data['stores'] = array();
		
		$this->data['stores'][] = array(
			'store_id'			=> 0,
			'name'     			=> $this->config->get('config_name') . $this->language->get('text_default')
			);
			
		$this->load->model('setting/store');
		
		$results = $this->model_setting_store->getStores();
		foreach($results as $result)
			$this->data['stores'][] = array(
				'store_id' 			=> $result['store_id'],
				'name'				=> $result['name']
			);
		
		
		$this->template = 'module/watermark.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}
	
	public function generate()
	{
		
		$text = urlencode($this->request->get['text']);
		
		$font = "../fonts/".$this->request->get['font'];
		
		$size = $this->request->get['size'];
		
		$t_color=$this->request->get['color'];
		
		$angle=0;
		
		$alpha=$this->request->get['alpha'];
		
		$ary=imagettfbbox($size, $angle, $font, $text);
		$width=abs($ary[4]-$ary[0])+5;
		$height=abs($ary[5]-$ary[1])+5;
		
		
		$dst=imagecreatetruecolor($width,$height);
			
		$trans=imagecolorallocate($dst,255,255,255);
		
		imagecolortransparent($dst,$trans);
		
		imagefill($dst,0,0,$trans);
		
		$color=imagecolorallocatealpha($dst,base_convert(substr($t_color,0,2),16,10),base_convert(substr($t_color,2,2),16,10),base_convert(substr($t_color,4,2),16,10),(100-$alpha)*127/100);
		
		imagettftext($dst,$size,$angle,0,$size,$color,$font,$text);
		

		
		$this->response->addHeader("Content-Type: image/png");
		$this->response->setOutput(imagepng($dst));
	}
	
	public function upload()
	{
		if (!empty($this->request->files)) {
			
			$tempFile = $this->request->files['Filedata']['tmp_name'];
			$targetFile = DIR_IMAGE. 'watermark/watermark_temp';
			
			$filename = $this->request->files['Filedata']['name'];
			
			$fileParts = pathinfo($filename);
			
			if ($fileParts['extension'] == "png") 
			{
				move_uploaded_file($tempFile,$targetFile.'.'.$fileParts['extension']);
				$this->response->setOutput('2');				
			}
			else
			{
				$this->response->setOutput('1');				
			}
			return;
		}
		$this->response->setOutput('0');
	}
	
	/*public function apply()
	{
		$this->load->model('module/watermark');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() && $this->model_module_watermark->updateApplySetting($this->request->post)) {
		
			$this->model_module_watermark->clearCache(DIR_IMAGE . 'cache');	
			$this->session->data['success'] = 'Success: You have applied new watermark settings!';
			$this->redirect($this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'));
		}
		
		
		if (isset($this->session->data['warning'])) {
			$this->data['error_warning'] = $this->session->data['warning'];
			unset($this->session->data['warning']);
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Home',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Modules',
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Watermark Modification',
			'href'      => $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Applying Watermark',
			'href'      => $this->url->link('module/watermark/apply', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		
		$this->data['action'] = $this->url->link('module/watermark/apply', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		
		$this->load->model('catalog/manufacturer');
		$this->load->model('setting/store');
		
		$this->data['stores'] = array();
		
		$this->data['stores'][] = array(
			'store_id'			=> 0,
			'name'     			=> $this->config->get('config_name') . $this->language->get('text_default'),
			'categories' 		=> $this->model_module_watermark->getCategoriesByStore('0')
			);
		//print_r($this->model_module_watermark->getCategoriesByStore('0'));
		
		$results = $this->model_setting_store->getStores();
		foreach($results as $result)
			$this->data['stores'][] = array(
				'store_id' 			=> $result['store_id'],
				'name'				=> $result['name'],
				'categories' 			=> $this->model_module_watermark->getCategoriesByStore($result['store_id'])
			);
		
		$this->data['watermarks'] = $this->model_module_watermark->getWatermarks();		
		
		if(isset($this->request->post['store_watermark']))
			$this->data['savedval'] = $this->request->post['store_watermark'];
		else
			$this->data['savedval'] = $this->model_module_watermark->getApplySetting();
		
		$this->template = 'module/watermark_apply.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}
	*/
	/*public function getChildCategory()
	{
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() && isset($this->request->post['store']) && isset($this->request->post['category'])) 
		{
		
			$this->load->model('module/watermark');
			
			$this->data['categories'] = $this->model_module_watermark->getCategoriesByStore($this->request->post['store'], $this->request->post['category']);
			
			$this->data['watermarks'] 	= $this->model_module_watermark->getWatermarks();	
			$this->data['store'] 		= $this->request->post['store'];
			$this->data['category'] 	= $this->request->post['category'];
			
			$this->template = 'module/watermark_cate_child.tpl';
				
			$this->response->setOutput($this->render());	
		}
	}
	*/
		
	public function add()
	{
		$this->load->model('module/watermark');
		$this->load->model('tool/image');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() && $this->model_module_watermark->addWatermark($this->request->post)) {
			$this->model_module_watermark->clearCache(DIR_IMAGE . 'cache');	
			$this->session->data['success'] = 'Success: You have created new watermark!';
			$this->redirect($this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'));
		}
		
		
		if (isset($this->session->data['warning'])) {
			$this->data['error_warning'] = $this->session->data['warning'];
			unset($this->session->data['warning']);
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Home',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Modules',
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Watermark Modification',
			'href'      => $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Create New Watermark',
			'href'      => $this->url->link('module/watermark/add', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/watermark/add', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->data['watermark_text_fonts'] = array();
		
		$watermark_text_fonts = glob("../fonts/*.ttf");
		
		foreach($watermark_text_fonts as $file)
		{
			$this->data['watermark_text_fonts'][] = substr( $file, ( strrpos( $file, "/" ) +1 ) );
		}		
		
		$this->data['no_image'] = $this->model_tool_image->resize('data/watermark.png', 100, 100);
		
		$this->template = 'module/watermark_add.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}
	
	
	public function update()
	{
		$this->load->model('module/watermark');
		$this->load->model('tool/image');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate() && $this->model_module_watermark->editWatermark($this->request->post, $this->request->get['wid'])) {
			$this->model_module_watermark->clearCache(DIR_IMAGE . 'cache');	
			$this->session->data['success'] = 'Success: You have updated new watermark!';
			$this->redirect($this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'));
		}
		
		
		if (isset($this->session->data['warning'])) {
			$this->data['error_warning'] = $this->session->data['warning'];
			unset($this->session->data['warning']);
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Home',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Modules',
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Watermark Modification',
			'href'      => $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Create New Watermark',
			'href'      => $this->url->link('module/watermark/update', 'token=' . $this->session->data['token'] . '&wid=' . $this->request->get['wid'], 'SSL'),
      		'separator' => ' :: '
   		);
		
				
		$this->data['action'] = $this->url->link('module/watermark/update', 'token=' . $this->session->data['token']. '&wid=' . $this->request->get['wid'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('module/watermark', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		$results = $this->model_module_watermark->getWatermarks();

		foreach ($results as $result){
			if($result['wid'] == $this->request->get['wid']){
				
				$result['watermark_thumb'] = $this->model_tool_image->resize($result['watermark_image'], 100, 100);
				$this->data['watermark'] = $result;			
			}
		}
		
			
		$this->data['watermark_text_fonts'] = array();
		
		$watermark_text_fonts = glob("../fonts/*.ttf");
		
		foreach($watermark_text_fonts as $file)
		{
			$this->data['watermark_text_fonts'][] = substr( $file, ( strrpos( $file, "/" ) +1 ) );
		}		
		
		$this->data['no_image'] = $this->model_tool_image->resize('data/watermark.png', 100, 100);
		
		$this->template = 'module/watermark_update.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/watermark')) {
			$this->error['warning'] = "Warning: You do not have permission to modify this module!";
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
	
	private function getManufacturersDetailByStoreid($store_id)
	{
		$this->load->model('module/watermark');
		$this->load->model('catalog/manufacturer');
		
		$data = array();
		
		$manufacturer_ids = $this->model_module_watermark->getManufacturersByStoreid($store_id);
		
		foreach($manufacturer_ids as $id)
			$data[] = $this->model_catalog_manufacturer->getManufacturer($id);
			
		return $data;
	}
}
?>